<template>
  <div class="login">
    <van-nav-bar
      title="账号登录"
      left-text=""
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
        <div class="am-flexbox-item">
          <a href="#/registe">还没有账号，去注册~</a>
        </div>
      </div>
    </van-form>
    <!-- /form表单区域 -->
    <!-- 注册区域 -->
  </div>
</template>

<script>
import { getLogin } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },

  created () {},

  methods: {
    async onSubmit () {
      const loading = Toast.loading({
        message: '登陆中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        const res = await getLogin(this.form)
        console.log(res)
        this.$store.commit('initUser', res.data.body.token)
        loading.clear()
        Toast('登陆成功！ ')
        this.$router.push('/my')
      } catch (error) {
        Toast('登陆失败！ ')
      }
    },
    onClickLeft () {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="less">
.login {
  .van-nav-bar {
    background-color: #21b97a;
    /deep/.van-icon-arrow-left {
      color: #fff;
    }
    /deep/.van-nav-bar__title {
      color: #fff;
    }
  }
  .van-button--info {
    background-color: #21b97a;
    border: 1px solid #21b97a;
  }
  .am-flexbox-item {
    margin-top: 20px;
    margin-left: 100px;
    a {
      color: #666;
      font-size: 14px;
    }
  }
}
</style>
